package com.vdong.common.oauth.common.authorize;

import com.vdong.kpay.basics.dubbo.constant.common.PlatformEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 刀锋
 * @date : Create in 11:59 2018/6/25
 * @description:
 */
@Slf4j
public class AuthWebAuthenticationDetails extends WebAuthenticationDetails {

    private static final long serialVersionUID = -4510054628034604015L;
    /**
     * 用户类型
     * 10：saas 平台用户
     * 20：店铺管理后台用户
     */
    private Integer userType;

    private Long userId;

    /**
     * 店铺id
     */
    private Long storeId;

    /**
     * 登录域名
     */
    private String domain;

    /**
     * 验证码
     */
    private String verifyCode;

    /**
     * 登录方式
     */
    private LoginWay loginWay;

    /**
     * //登录平台
     */
    private PlatformEnum platform;


    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getStoreId() {
        return storeId;
    }

    public void setStoreId(Long storeId) {
        this.storeId = storeId;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }

    public LoginWay getLoginWay() {
        return loginWay;
    }

    public void setLoginWay(LoginWay loginWay) {
        this.loginWay = loginWay;
    }

    public PlatformEnum getPlatform() {
        return platform;
    }

    public void setPlatform(PlatformEnum platform) {
        this.platform = platform;
    }

    /**
     * Records the remote address and will also set the session Id if a session already
     * exists (it won't create one).
     *
     * @param request that the authentication request was received from
     */
    public AuthWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        String serverName = request.getServerName();
        this.domain = serverName;
        log.debug("=========================> ServerName:{}", serverName);
        String storeIdStr = request.getParameter("storeId");
        //用户类型
        String userTypeStr = request.getParameter("userType");
        if (!StringUtils.isEmpty(userTypeStr)) {
            this.userType = new Integer(userTypeStr);
        }
        if (AuthCommon.User.SAAS_USER == this.userType) {
            this.platform = PlatformEnum.SAAS;
        }
        if (AuthCommon.User.STORE_USER == this.userType) {
            this.platform = PlatformEnum.WEB;
        }
        //登录方式
        String loginWayStr = request.getParameter("loginWay");
        this.loginWay = LoginWay.findBy(NumberUtils.toInt(loginWayStr));
//        String storeIdStr = request.getParameter("storeId");
        if (!StringUtils.isEmpty(storeIdStr)) {
            this.storeId = new Long(storeIdStr);
        }
        this.verifyCode = request.getParameter("verifyCode");
    }

    /**
     * 登录方式
     */
    enum LoginWay {
        //密码
        PASSWORD(10, "pwd"),
        //验证码
        VERIFYCODE(20, "code");

        private int code;
        private String type;

        LoginWay(int code, String type) {
            this.code = code;
            this.type = type;
        }

        public int getCode() {
            return code;
        }

        public String getType() {
            return type;
        }

        public static LoginWay findBy(int code) {
            LoginWay[] allways = values();
            for (LoginWay way : allways) {
                if (way.code == code) {
                    return way;
                }
            }
            //默认密码方式
            return PASSWORD;
        }

    }
}
